package org.platform.lwc.tool.auth.holder;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import org.platform.lwc.tool.auth.constants.AuthConstant;
import org.platform.lwc.tool.auth.domain.LoginUser;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

;

/**
 * 获取登录用户信息
 */
public class LoginUserHolder {

    public static LoginUser getCurrentUser(){
        //从Header中获取用户信息
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String userStr = request.getHeader(AuthConstant.BASIC_HEADER_LOGINUSER);
        JSONObject userJsonObject = new JSONObject(userStr);
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(userJsonObject.getStr("user_name"));
        loginUser.setId(userJsonObject.getStr("id"));
        loginUser.setRoles(Convert.toList(String.class,userJsonObject.get("authorities")));
        return loginUser;
    }

}
